﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace YJKArchMethodLibrary.StructurePoleSuit
{
	public class Structure2DNode
	{
		public XYZ Position
		{
			get
			{
				return this.m_ptPosition;
			}
			set
			{
				this.m_ptPosition = value;
			}
		}

		public string Tag
		{
			get
			{
				return this.m_Tag;
			}
			set
			{
				this.m_Tag = value;
			}
		}

		public List<Structure2DPole> PoleList
		{
			get
			{
				return this.m_PoleList;
			}
		}

		public void AddPole(Structure2DPole pole)
		{
			this.m_PoleList.Add(pole);
		}

		public void RemoveEdge(int index)
		{
			if (index >= 0 && index < this.m_PoleList.Count)
			{
				this.m_PoleList.RemoveAt(index);
				this.AnticlockwiseSortElements();
			}
		}

		public void RemoveSortEdges(List<int> indexList)
		{
			for (int i = indexList.Count - 1; i >= 0; i--)
			{
				int num = indexList[i];
				if (num >= 0 && num < this.m_PoleList.Count)
				{
					this.m_PoleList.RemoveAt(num);
				}
			}
		}

		public Structure2DPole GetPoleByIndex(int index)
		{
			if (index >= 0 && index < this.m_PoleList.Count)
			{
				return this.m_PoleList[index];
			}
			return null;
		}

		public Structure2DPole GetPreviusPoleByIndex(int index)
		{
			if (index < 0 || index >= this.m_PoleList.Count)
			{
				return null;
			}
			if (index == 0)
			{
				return this.m_PoleList[this.m_PoleList.Count - 1];
			}
			return this.m_PoleList[index - 1];
		}

		public Structure2DPole GetNextPoleByIndex(int index)
		{
			if (index < 0 || index >= this.m_PoleList.Count)
			{
				return null;
			}
			if (index == this.m_PoleList.Count - 1)
			{
				return this.m_PoleList[0];
			}
			return this.m_PoleList[index + 1];
		}

		public void AnticlockwiseSortElements()
		{
			this.m_PoleList.Sort(new AngleComparer(this.m_ptPosition, 0.0));
		}

		public List<int> FindLinkPoleWithOtherNode(XYZ ptOtherNode)
		{
			List<int> list = new List<int>();
			for (int i = 0; i < this.m_PoleList.Count; i++)
			{
				Structure2DPole structure2DPole = this.m_PoleList[i];
				XYZ xyz = structure2DPole.CurOutNodePosition(this.m_ptPosition);
				if (xyz != null && ptOtherNode.IsAlmostEqualTo(xyz, 0.0001))
				{
					list.Add(i);
				}
			}
			return list;
		}

		public int FindPoleIndex(Structure2DPole findPole)
		{
			int result = -1;
			for (int i = 0; i < this.m_PoleList.Count; i++)
			{
				Structure2DPole rhs = this.m_PoleList[i];
				if (findPole.SameAs(rhs))
				{
					result = i;
					break;
				}
			}
			return result;
		}

		public Structure2DNode(Structure2DNode rhs)
		{
			this.m_ptPosition = rhs.m_ptPosition;
			this.m_PoleList = rhs.m_PoleList;
			this.m_Tag = rhs.m_Tag;
		}

		public Structure2DNode(XYZ ptPosition, List<Structure2DPole> poleList)
		{
			this.m_ptPosition = ptPosition;
			this.m_PoleList = poleList;
			this.m_Tag = "";
		}

		private XYZ m_ptPosition;

		private List<Structure2DPole> m_PoleList = new List<Structure2DPole>();

		private string m_Tag = "";
	}
}
